<?php

class Default_Controllers_Product extends Libs_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $pro = new Default_Models_Product();
        if (isset($_GET['keyword'])) {

            function remove_unicode($str) {
                if ($str == '')
                    return false; $str = trim($str);
                $chars = array('a' => array('ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'á', 'à', 'ả', 'ã', 'ạ', 'â', 'ă', 'Á', 'À', 'Ả', 'Ã', 'Ạ', 'Â', 'Ă'), 'e' => array('ế', 'ề', 'ể', 'ễ', 'ệ', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê'), 'i' => array('í', 'ì', 'ỉ', 'ĩ', 'ị', 'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'î'), 'o' => array('ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'Ố', 'Ồ', 'Ổ', 'Ô', 'Ộ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ơ', 'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ơ'), 'u' => array('ứ', 'ừ', 'ử', 'ữ', 'ự', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư'), 'y' => array('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'), 'd' => array('đ', 'Đ'), '' => array('/', '\\', ',', '.', '"', '\"', '-', "&quot;", '*', '{', '}', '<', '>', '(', ')', '&lt;', '&gt;', '?', "'", "\'", '~', '#', '^', '“', '”', ':', ';', '&', '&amp;', '+', '=', '%', '$', '@', '!', "'"), 'pc' => array('%'), '-' => array(' ', '%20', '_'));
                foreach ($chars as $key => $arr)
                    foreach ($arr as $val)
                        $str = str_replace($val, $key, $str);
                return $str;
            }

//            $keyword = preg_replace('/[^A-Za-z0-9]/i', ' ', mysql_real_escape_string(strtolower(remove_unicode($_GET['keyword']))));
            $keyword = $_GET['keyword'];
            $this->view->msg = "Kết quả tìm kiếm";
            $this->view->listPro = $pro->getProductsByKeyword($keyword);
            $this->view->render('product/index');
        } else {
            $this->view->msg = "Sản phẩm mới";
            $this->view->listPro = $pro->getNewProducts();
            $this->view->render('product/index');
        }
    }

    public function detail($id) {
        $pro = new Default_Models_Product();
        $this->view->catName = $pro->getCatNameByProId($id);
        $this->view->proDetail = $pro->getProDetail($id);
        $this->view->render('product/detail');
    }

    public function category($id) {
        $pro = new Default_Models_Product();
        $this->view->msg = $pro->getCatNameById($id);
        $this->view->proByCat = $pro->getProductsByCat($id);
        $this->view->render('product/product-cat');
    }
    
    public function sales() {
        $pro = new Default_Models_Product();
        $this->view->msg = "Sản phẩm giảm giá";
        $this->view->salePro = $pro->getSaleProducts();
        $this->view->render('product/sale-products');
    }

}
